package com.david.equipment.mapper;

import com.david.equipment.pojo.entity.*;
import com.david.equipment.pojo.vo.*;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

public interface TacticsMapper {

    @Insert("insert into equip_tactics (name, code,type,status, loop_period,duration,project_id, dept_id, start_date, end_date, create_time, createby) values " +
            "(#{name},#{code},#{type},#{status}, #{loopPeriod},#{duration},#{projectId},#{deptId},#{startDate},#{endDate},#{createTime},#{createby})")
    @SelectKey(resultType = Integer.class, keyProperty = "id", before = false, keyColumn = "id",statement = "SELECT LAST_INSERT_ID()")
    void insertTactics(TacticsVO tacticsVO);

    void insertTacticsDevices(TacticsVO tacticsVO);

    List<TacticsEntity> selectTactics(TacticsEntity tacticsEntity);

    @Select("select id, tactics_id, device_id, status, remark from equip_tactics_device where tactics_id = #{tacticsId} and status = #{status}")
    List<TacticsDevice> selectTacticsDevices(Integer tacticsId, String status);

    @Insert("insert into equip_tactics_task (tactics_id, tactics_name, code, start_date, end_date) values " +
            "(#{tacticsId},#{tacticsName},#{code},#{startDate},#{endDate})")
    @SelectKey(resultType = Integer.class, keyProperty = "id", before = false, keyColumn = "id",statement = "SELECT LAST_INSERT_ID()")
    void insertTask(TacticsTaskEntity task);

    @Insert("insert into equip_tactics_task_device (task_id, device_id, remark) VALUES (#{taskId},#{deviceId},#{remark})")
    void insertTacticsTaskDevices(TacticsTaskDeviceEntity taskDevice);

    @Select("select id,project_id, name,content,remark from equip_project_items where project_id = #{projectId}")
    List<TacticsProjectItems> selectItems(Integer projectId);

    @Insert("insert into equip_tactics_task_items (task_id, device_id, name, content, result, remark) values (#{taskId},#{deviceId},#{name},#{content},#{result},#{remark})")
    void insertTacticsTaskItems(TacticsTaskItems taskItem);

    @Insert("insert into equip_project (name, type, dept_id) VALUES (#{name},#{type},#{deptId})")
    @SelectKey(resultType = Integer.class, keyProperty = "id", before = false, keyColumn = "id",statement = "SELECT LAST_INSERT_ID()")
    void inertProject(TacticsProjectVO tacticsProjectVO);


    void insertProjectItems(List<TacticsProjectItems> itemsList);

    @Select("select id, tactics_id, tactics_name, code,dept_id, start_date, end_date from equip_tactics_task where end_date < #{now}")
    List<TacticsTaskEntity> selectTacticsTaskOutTime(LocalDate now);


    List<TacticsProject> selectProject(String type, Integer deptId);

    @Select("select id, project_id, name, content, remark from equip_project_items where project_id = #{projectId}")
    List<TacticsProjectItems> selectProjectItems(Integer projectId);

    @Update("update equip_project set name = #{name}, type = #{type}, dept_id = #{deptId} where id = #{id}")
    void updateProject(TacticsProject project);

    @Update("update equip_project_items set name = #{name}, content = #{content}, remark = #{remark}  where id = #{id} and project_id = #{projectId}")
    void updateProjectItems(TacticsProjectItems item);

    @Delete("delete from equip_project where id = #{id}")
    void deleteProject(Integer id);

    @Delete("delete from equip_project_items where project_id = #{id}")
    void deleteProjectItems(Integer id);

    // 任务归集Mapper

    List<TacticsTaskItems> selectTacticsTaskItems(TacticsTaskItems taskItem);

    List<TacticsTaskDeviceEntity> selectTacticsTaskDevices(TacticsTaskDeviceEntity taskDevice);

    void insertTacticsHisItems(List<TacticsHisTaskItems> hisTaskItems);

    @Delete("delete from equip_tactics_task_items where task_id = #{taskId}")
    void deleteTacticsTaskItems(Integer taskId);

    @Delete(("delete from equip_tactics_task_device where task_id = #{taskId}"))
    void deleteTacticsTaskDevice(Integer taskId);

    @Delete("delete from equip_tactics_task where id = #{taskId}")
    void deleteTacticsTask(Integer taskId);

//    @Select("select id, task_id, device_id, name, content, result, remark from equip_tactics_task_items where task_id = #{taskId}")
    List<TacticsTaskItems> selectTacticsHisItems(TacticsTaskItems hisItem);

    void insertTacticsHisTask(TacticsHisTaskEntity hisTask);

    List<TacticsHisTaskEntity> selectTacticsHisTask(TacticsHisTaskEntity hisTask);

    @Select("select device_id,name as device_name, equip_tactics_history_task.code as code from equip_tactics_history_task left join equip_device on device_id = equip_device.id where equip_tactics_history_task.dept_id = #{deptId} and start_date <= #{now} and end_date >= #{now} and equip_tactics_history_task.code like concat(#{preCode},'%')")
    List<TacticsNowTaskVO.completedTask> selectNowHisTask(LocalDate now, Integer deptId, String preCode);

    @Select("select equip_tactics_task_device.id,equip_tactics_task_device.task_id, device_id,name as device_name from equip_tactics_task_device left join\n" +
            "   equip_device on equip_tactics_task_device.device_id = equip_device.id where equip_tactics_task_device.task_id in (select id from equip_tactics_task where dept_id = #{deptId} and code like concat(#{preCode},'%'))")
    List<TacticsNowTaskVO.uncompletedTask> selectNowTask(Integer deptId, String preCode);

    void deleteTacticsTaskItemsByIds(List<Integer> ids);

    void deleteTacticsTaskDeviceByEntity(TacticsTaskDeviceEntity taskDevice);

    @Select("select id, tactics_id, tactics_name, code, dept_id, start_date, end_date from equip_tactics_task where id = #{taskId}")
    TacticsTaskEntity selectTacticstaskById(Integer taskId);

    void updateTacticsStatusByIds(List<Integer> ids, @Param("tactics") TacticsEntity tactics);

    @Select("select etd.id, tactics_id, device_id, status, remark, name as device_name, code as device_code from equip_tactics_device etd left join equip_device ed on etd.device_id = ed.id where etd.tactics_id = #{id}")
    List<TacticsDeviceVO> selectTacticsDevicesWithDeviceName(Integer id);

    void deleteTacticsByIds(List<Integer> tacticsIds);

    void deleteTacticsDeviceByTacticsIds(List<Integer> tacticsIds);

    void deleteTacticsProjectByIds(List<Integer> tacticsIds);

    @Select("select id, name, type, dept_id from equip_project where id = #{projectId}")
    TacticsProjectVO selectProjectById(Integer projectId);

    @Update("update equip_tactics set name = #{name}, loop_period = #{loopPeriod}, duration = #{duration}, project_id = #{projectId}, dept_id = #{deptId}, start_date = #{startDate}, end_date = #{endDate}, update_time = #{updateTime}, updateby = #{updateby} where id = #{id}")
    void updateTactics(TacticsVO tacticsVO);

    @Update("update equip_tactics_device set status = #{status}, remark = #{remark} where tactics_id = #{tacticsId} and device_id = #{deviceId}")
    void updateTacticsDevice(TacticsDeviceEntity device);

    @Select("select id, tactics_id, device_id, status, remark from equip_tactics_device where tactics_id = #{tacticsId}")
    List<TacticsDevice> selectTacticsDevicesByTacticsId(Integer tacticsId);

    @Insert("insert into equip_tactics_device (tactics_id, device_id, status, remark) VALUES (#{tacticsId}, #{deviceId},#{status},#{remark})")
    void insertTacticsDevicesByEntity(TacticsDeviceEntity device);

    void deleteTacticsDeviceByIds(Integer tacticsId, List <Integer> oldIds);
}
